package jiangzuomanage.dao;

import jiangzuomanage.model.JiangZuo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class JiangZuoDaoImpl implements JiangZuoDao {
    @Override
    public boolean addJiangZuo(JiangZuo jiangZuo) {
        Connection connection = DbUtil.getConnection();// 获得数据库连接对象
        String sql = "insert into t_jiangzuo(id,name,place,person,time)values(?,?,?,?,?)";
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, jiangZuo.getId());
            ps.setString(2, jiangZuo.getName());
            ps.setString(3, jiangZuo.getPlace());
            ps.setString(4, jiangZuo.getPerson());
            ps.setString(5, jiangZuo.getTime());
            if (!ps.execute()) {// 成功
                DbUtil.close(connection, ps);// 关闭连接
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;// 失败
    }

    @Override
    public boolean deleteJiangZuo(JiangZuo jiangZuo) {
        Connection connection = DbUtil.getConnection();// 获得数据库连接对象
        String sql = "delete from  t_jiangzuo where id=?";
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, jiangZuo.getId());
            if (!ps.execute()) {// 成功
                DbUtil.close(connection, ps);// 关闭连接
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;// 失败
    }

    @Override
    public boolean updateJiangZuo(JiangZuo jiangZuo) {
        Connection connection = DbUtil.getConnection();// 获得数据库连接对象
        String sql = "update  t_jiangzuo set name=?,place=?,person=?,time=? where id=?";
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, jiangZuo.getName());
            ps.setString(2, jiangZuo.getPlace());
            ps.setString(3, jiangZuo.getPerson());
            ps.setString(4, jiangZuo.getTime());
            ps.setString(5, jiangZuo.getId());
            if (!ps.execute()) {// 成功
                DbUtil.close(connection, ps);// 关闭连接
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;// 失败
    }

    @Override
    public List<JiangZuo> findJiangZuo(JiangZuo jiangZuo) {
        List<JiangZuo> jiangZuos = new ArrayList<>();
        try {
            Connection connection = DbUtil.getConnection();
            String sql = "select id,name,place,person,time from t_jiangzuo";
            PreparedStatement ps = connection.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {// 存在用户，封装用户返回
                JiangZuo tmpJiangZuo = new JiangZuo(rs.getString("id"), rs.getString("name"),
                        rs.getString("time"), rs.getString("person"), rs.getString("place"));
                jiangZuos.add(tmpJiangZuo);
            }
            DbUtil.close(connection, ps);// 关闭连接
            return jiangZuos;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public JiangZuo findById(JiangZuo jiangZuo) {
        List<JiangZuo> jiangZuos = new ArrayList<>();
        try {
            Connection connection = DbUtil.getConnection();
            String sql = "select id,name,place,person,time from t_jiangzuo where id=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, jiangZuo.getId());
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {// 存在用户，封装用户返回
                JiangZuo tmpJiangZuo = new JiangZuo(rs.getString("id"), rs.getString("name"),
                        rs.getString("time"), rs.getString("person"), rs.getString("place"));
                return tmpJiangZuo;

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
